package com.vue.controller.student;

import com.vue.constant.JwtClaimsConstant;
import com.vue.constant.MessageConstant;
import com.vue.context.BaseContext;
import com.vue.dto.NewPasswordDTO;
import com.vue.dto.LoginDTO;
import com.vue.dto.StudentDTO;
import com.vue.entity.Student;
import com.vue.entity.Teacher;
import com.vue.exception.PasswordErrorException;
import com.vue.properties.JwtProperties;
import com.vue.result.Result;
import com.vue.service.StudentService;
import com.vue.utils.JwtUtil;
import com.vue.vo.UserInfoVO;
import com.vue.vo.StudentLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/student")
@RestController
@Api(tags = "学生相关接口")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 学生登录
     * @param LoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("学生登录")
    public Result<StudentLoginVO> login(@RequestBody LoginDTO LoginDTO) {
        log.info("学生登录：{}", LoginDTO);
        Student student = studentService.login(LoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.STUDENT_ID, student.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getStudentSecretKey(),
                jwtProperties.getStudentTtl(),
                claims);

        StudentLoginVO studentLoginVO = new StudentLoginVO();
        studentLoginVO.setId(student.getId());
        studentLoginVO.setUsername(student.getUsername());
        studentLoginVO.setName(student.getName());
        studentLoginVO.setToken(token);
        return Result.success(studentLoginVO);
    }

    @GetMapping("/userInfo")
    @ApiOperation("获取学生信息")
    public Result<UserInfoVO> userInfo() {
        UserInfoVO userInfoDTO = new UserInfoVO();
        Student student = studentService.getUserInfo(userInfoDTO);
        BeanUtils.copyProperties(student, userInfoDTO);
        return Result.success(userInfoDTO);
    }

    /**
     * 学生找回密码
     * @param forgetPasswordDTO
     * @return
     */
    @PutMapping("/forgetPassword")
    @ApiOperation("学生找回密码")
    public Result forgetPassword(@RequestBody NewPasswordDTO forgetPasswordDTO){
        log.info("学生找回密码：{}", forgetPasswordDTO);
        studentService.forgetPassword(forgetPasswordDTO);
        return Result.success();
    }

    /**
     * 学生修改信息
     * @param studentDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("学生修改信息")
    public Result updateInfo(@RequestBody StudentDTO studentDTO) {
        log.info("学生修改信息：{}", studentDTO);
        studentService.update(studentDTO);
        return Result.success();
    }

    /**
     * 教师修改密码
     * @param newPasswordDTO
     * @return
     */
    @PatchMapping("/updatePwd")
    @ApiOperation("学生修改密码")
    public Result updatePwd(@RequestBody NewPasswordDTO newPasswordDTO) {
        log.info("学生修改密码：{}", newPasswordDTO);
        //1.校验参数
        String oldPwd = newPasswordDTO.getOldPassword();
        String newPwd = newPasswordDTO.getNewPassword();
        String rePwd = newPasswordDTO.getRePassword();

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要输入");
        }

        //原密码是否正确
        //调用userService根据用户名拿到原密码，再和old_pwd比对
        Long teacherId = BaseContext.getCurrentId();
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setId(teacherId);
        Student student = studentService.getUserInfo(userInfoVO);
        if (!student.getPassword().equals(DigestUtils.md5DigestAsHex(oldPwd.getBytes()))) {
            throw new PasswordErrorException(MessageConstant.OLD_PASSWORD_ERROR);
        }

        //newPwd和rePwd是否一样
        if (!rePwd.equals(newPwd)){
            throw new PasswordErrorException(MessageConstant.NEW_PASSWORD_ERROR);
        }

        if (newPwd.length() < 5 || newPwd.length() > 16) {
            throw new PasswordErrorException(MessageConstant.NEW_PASSWORD_ERROR);
        }

        //2.调用service完成密码更新
        studentService.updatePwd(newPwd);
        return Result.success();
    }
}

















